package tests;

import common.Utils;
import org.checkerframework.checker.units.qual.K;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-04-28
 * Time: 11:23
 */
/*
帖子详情页的测试
 */
public class PostDetailsPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public PostDetailsPage() {
        super(url);
    }
    // 首页打开测试
    public void HomeOpenSuc() {
        // 需要重新登录
        // 返回登录页并刷新页面
        driver.navigate().refresh();
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 获取界面元素
        String ele1 = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        // 断言判断是否打开成功
        assert ele1.equals("首页");
    }

    // 点击打开帖子, 查看详情
    public void PostOpen() {
        // 点击帖子标题, 进入帖子详情页
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        // 获取页面元素
        String ele2 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(2) > h3")).getText();
        // 断言判断是否进入帖子详情页
        assert ele2.equals("最新回复");
    }

    // 点赞功能测试
    public void PostThumbsUp() {
        // 点击点赞按钮
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        // 获取当前点赞数
        String ele3 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")).getText();
        // 断言判断是否正确
        assert ele3.equals("4");
    }

    // 编辑功能测试
    // 编辑页面进入测试
    public void PostRewriteEnter() throws InterruptedException {
        Thread.sleep(1000);
        // 点击编辑按钮, 进入编辑界面
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(2) > div")).click();
        // 获取界面元素
        Thread.sleep(1000);
        String ele4 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")).getText();
        // 断言判断是否进入成功
        assert ele4.equals("编辑");
    }

    // 编辑内容测试
    public void PostRewrite() throws InterruptedException {
        // 找到标题框元素
        WebElement ele5 = driver.findElement(By.cssSelector("#edit_article_title"));
        // 点击帖子标题框
        Actions actions = new Actions(driver);
        actions.click(ele5).perform();
        // 清空原有内容
//        actions.keyDown(Keys.BACK_SPACE).perform();
//        driver.findElement(By.cssSelector("#edit_article_title")).sendKeys("");
        actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
        actions.sendKeys("").perform();
        // 输入修改内容
        // 也可以用 action.sendKeys();
        driver.findElement(By.cssSelector("#edit_article_title")).sendKeys("1.5");
        // 获取修改后的内容
//        String ele6 = driver.findElement(By.cssSelector("#edit_article_title")).getText();
        // 找到帖子详情内容修改框
        WebElement ele7 = driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
        // 点击内容修改框
        actions.click(ele7).perform();
        // 清空原有内容
//        Thread.sleep(1000);
//        actions.keyDown(Keys.BACK_SPACE).perform();
//        driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("");
        actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
        actions.sendKeys("").perform();
        // 输入要修改的内容
//        driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("2.01");
        actions.sendKeys("1.50").perform();
        // 点击提交按钮
        Thread.sleep(1000);
        WebElement ele8 = driver.findElement(By.cssSelector("#edit_article_submit"));
        actions.click(ele8).perform();
        // 获取新帖子标题信息
        Thread.sleep(2000);
        String ele9 = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate")).getText();
        // 断言判断修改后的帖子标题, 看是否和修改内容相同
        assert ele9.equals("1.5");
    }

    // 删除功能测试
    public void PostDelete() {
        // 进入帖子详情页
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        // 点击删除按钮
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        // 点击确认按钮
        driver.findElement(By.cssSelector("#details_artile_delete")).click();
        // 通过获取新界面的 url 来通过断言判断是否删除成功返回首页
        String homeUrl = driver.getCurrentUrl();
        assert homeUrl.equals("http://127.0.0.1:58080/index.html");
    }

    // 回复功能测试
    public void PostReply() throws InterruptedException {
        // 进入帖子详情页
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        // 找到回复输入框元素
        WebElement ele9 =driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        // 点击输入框
        Actions actions = new Actions(driver);
        actions.click(ele9).perform();
        // 输入想要发送的内容
        Thread.sleep(1000);
        actions.sendKeys("11").perform();
        //driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")).sendKeys("测试回复1");
        // 点击回复
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#details_btn_article_reply")).click();
    }

}
